package com.pb.infra.param.controller;

import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import com.pb.infra.param.service.ImportConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 导入配置管理
 * @author liudebao
 * @date 2021/02/22
 */
@Slf4j
@RestController
@RequestMapping("v1/metadata/importConfig")
public class ImportConfigController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private ImportConfigService importConfigService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "DATA_IMP_CONF";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "导入配置管理";

    /**
     * 查询分页数据
     *
     * @param map     查询参数
     * @param page    页码
     * @return Result 返回数据
     */
    @GetMapping("selectByPage")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectByPage(@RequestParam Map<String, Object> map, Page page, HttpServletRequest request) {
        return importConfigService.selectByPage(map, page, request);
    }

    /**
     * 获取导入配置下拉菜单
     * @return Result 返回数据
     */
    @GetMapping("getImportConfigList")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectByPage() {
        return importConfigService.getImportConfigList();
    }

    /**
     * 保存导入配置信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        return importConfigService.insert(paramMap, request);
    }

    /**
     * 修改导入配置信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String, Object> paramMap, HttpServletRequest request){
        return importConfigService.update(paramMap, request);
    }

    /**
     * 查询数据库用户
     *
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("getBaseUserList")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getBaseUserList(@RequestBody Map<String,Object> map) {
        return importConfigService.getBaseUserList(map);
    }

    /**
     * 查询源表字段集合
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("getColList")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getColList(@RequestBody Map<String,Object> map) {
        return importConfigService.getColList(map);
    }

    /**
     * 删除导入配置信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<String> paramMap) {
        importConfigService.delete(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 查询字段函数
     * @return Result 返回数据
     */
    @GetMapping("selFunc")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selFunc() {
        return importConfigService.selFunc();
    }

    /**
     * 导入excel文件
     * @param paramMap 请求参数
     * @param multRequest
     * @return Result 返回数据
     */
    @PostMapping("importExcel")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.IMP, menuName = OPERATION_MENU)
    public Result importExcel(@RequestParam Map<String, Object> paramMap, MultipartHttpServletRequest multRequest) {
        MultipartFile excelFile = multRequest.getFile("excel-file");
        return importConfigService.importExcel(excelFile, paramMap);
    }

    /**
     * 表对表导入
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("importFileData")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.IMP, menuName = OPERATION_MENU)
    public Result importFileData(@RequestParam Map<String, Object> paramMap) {
        return importConfigService.importFileData(paramMap);
    }

}
